class Solution {
    public int sum(int n){
        int sum=0;
        while(n!=0){
            int m=n%10;
            sum+=m*m;
            n=n/10;
        }
        return sum;

    }
    public boolean isHappy(int n) {
        int slow=n;
        int fast=sum(n);
        while(slow!=fast){
            slow=sum(slow);
            fast=sum(sum(fast));
        }
        return slow==1;

    }
}